const express = require("express");
const app = express();

const bodyParser = require('body-parser');
//const { RSA_PKCS1_OAEP_PADDING } = require("constants");
app.use(bodyParser.json());

app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    if (req.method == "OPTIONS") res.send(200);
    else next();
});

const USERS = [
    { id: '01', userName: 'admin', password: '123456' },
    { id: '02', userName: 'lin', password: '123456' }
];


app.post('/user', function (req, resp) {
    console.log(req.body);
    var founded = false;
    for (let user of USERS) {
        if (user.userName === req.body.userName &&
            user.password === req.body.password) {
            founded = true;
            break;
        }
    }

    if (founded) {
        resp.send({ succ: true });
    } else {
        resp.send({ succ: false, msg: '没有账户！' });
    }
    resp.end();

});


app.listen(8080, function () {
    console.log('服务器在8080端口启动!');
});

